package com.mydemo.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SaaLLMConfig {

    //方法1：从环境变量中获取配置的apiKey
    // @Value("${spring.ai.dashscope.api-key}")
    // private String apiKey;
    //
    // @Bean
    // public DashScopeApi dashScopeApi(){
    //     return DashScopeApi.builder().apiKey(apiKey).build();
    // }

    //方法2：System.getenv("DASHSCOPE_API_KEY") 方式获取 apiKey,直接读取环境变量
    @Bean
    public DashScopeApi dashScopeApi(){
        return DashScopeApi.builder().apiKey(System.getenv("AI_DASHSCOPE_API_KEY")).build();
    }

    //chatClient 可以使用@Resource注解
    @Bean
    public ChatClient chatClient(ChatModel dashScopeModel){
        return ChatClient.builder(dashScopeModel).build();
    }
}
